using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;
using FiltronASP.Helpers.LastSearches;

namespace FiltronASP.Models.DAO
{
    public class LastSearchesDAO : Abstracts.AbstractGenericDAO<LastSearch, System.Nullable<int>>,
        Interfaces.ILastSearchesDAO
    {
        protected override string GetKeyProperty()
        {
            return "last_search_id";
        }

        public bool makeInsert(string lastSearchName, string lastSearchLink, int languageId)
        {
            LastSearch lastSearchToInsert;
            lastSearchToInsert = new LastSearch();
            lastSearchToInsert.last_search_name = lastSearchName;
            lastSearchToInsert.last_search_link = lastSearchLink;
            lastSearchToInsert.language_id = languageId;
            this.Add(lastSearchToInsert);
            return true;
        }

        public IQueryable<LastSearch> getLastSearches(int languageId) {
            if (languageId.Equals(0))
            {
                return null;
            }   //if
            try
            {

                IQueryable<LastSearch> lSearches = this._cmsEntitiesDataContext.LastSearches.Where(
                   ls => ls.language_id.Equals(languageId)
                        ).OrderByDescending(ls => ls.last_search_id).Take(50);
                if (!(lSearches.Any()))
                {
                    return null;
                }   //if
                return lSearches;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<LastSearch> getLastSearchesPaginated(int languageId, int paginated)
        {
            if (languageId.Equals(0))
            {
                return null;
            }   //if
            try
            {

                IQueryable<LastSearch> lSearches = this._cmsEntitiesDataContext.LastSearches.Where(
                   ls => ls.language_id.Equals(languageId)
                        ).OrderByDescending(ls => ls.last_search_id).Skip(paginated * 100).Take(100);
                if (!(lSearches.Any()))
                {
                    return null;
                }   //if
                return lSearches;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

    }
}
